package com.dianping.cat.alarm.server.transform;

import com.dianping.cat.alarm.server.IEntity;
import com.dianping.cat.alarm.server.entity.Condition;
import com.dianping.cat.alarm.server.entity.Rule;
import com.dianping.cat.alarm.server.entity.ServerAlarmRuleConfig;
import com.dianping.cat.alarm.server.entity.SubCondition;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;







public class DefaultJsonParser<T>
{
  private DefaultLinker m_linker;
  private Stack<String> m_tags;
  private Stack<Object> m_objs;
  private List<T> m_entities;
  private Class<?> m_entityClass;
  
  private DefaultJsonParser(Class<?> entityClass) {
/*  33 */     this.m_linker = new DefaultLinker(true);

/*  35 */     this.m_tags = new Stack();

/*  37 */     this.m_objs = new Stack();

/*  39 */     this.m_entities = new ArrayList();




/*  44 */     this.m_entityClass = entityClass;
  }

  
/*  48 */   public static <T extends IEntity<T>> T parse(Class<T> entityClass, InputStream in) throws IOException { return (T)parse(entityClass, new InputStreamReader(in, "utf-8")); }


  
  public static <T extends IEntity<T>> T parse(Class<T> entityClass, Reader reader) throws IOException {
/*  53 */     DefaultJsonParser parser = new DefaultJsonParser(entityClass);

/*  55 */     parser.onArrayBegin();
/*  56 */     parser.parse(new JsonReader(reader));
/*  57 */     parser.onArrayEnd();

/*  59 */     if (parser.m_entities.isEmpty()) {
/*  60 */       return null;
    }
/*  62 */     return (T)(IEntity)parser.m_entities.get(0);
  }


  
/*  67 */   public static <T extends IEntity<T>> T parse(Class<T> entityClass, String json) throws IOException { return (T)parse(entityClass, new StringReader(json)); }



/*  71 */   public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, InputStream in) throws Exception { return parseArray(entityClass, new InputStreamReader(in, "utf-8")); }


  
  public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, Reader reader) throws Exception {
/*  76 */     DefaultJsonParser parser = new DefaultJsonParser(entityClass);
    
    parser.parse(new JsonReader(reader));
/*  79 */     return (List<T>) parser.m_entities;
  }

  
/*  83 */   public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, String json) throws Exception { return parseArray(entityClass, new StringReader(json)); }



  
  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/*  89 */     if (value == null || value.length() == 0) {
/*  90 */       return defaultValue;
    }
    
/*  93 */     if (type == Boolean.class)
/*  94 */       return (T)Boolean.valueOf(value); 
/*  95 */     if (type == Integer.class)
/*  96 */       return (T)Integer.valueOf(value); 
/*  97 */     if (type == Long.class)
/*  98 */       return (T)Long.valueOf(value); 
/*  99 */     if (type == Short.class)
/* 100 */       return (T)Short.valueOf(value); 
/* 101 */     if (type == Float.class)
/* 102 */       return (T)Float.valueOf(value); 
/* 103 */     if (type == Double.class)
/* 104 */       return (T)Double.valueOf(value); 
/* 105 */     if (type == Byte.class)
/* 106 */       return (T)Byte.valueOf(value); 
/* 107 */     if (type == Character.class) {
/* 108 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 110 */     return (T)value;
  }

  
  private Object createRootEntity() {
    try {
/* 116 */       return this.m_entityClass.newInstance();
    
    }
/* 119 */     catch (Exception e) {
/* 120 */       throw new RuntimeException(String.format("Unable to create entity(%s) instance!", new Object[] { this.m_entityClass.getName() }), e);
    }
  }

  
/* 125 */   private boolean isTopLevel() { return (this.m_objs.size() == 1); }

  
  protected void onArrayBegin() {
/* 129 */     if (this.m_objs.isEmpty()) {
/* 130 */       this.m_objs.push(this.m_entities);
/* 131 */       this.m_tags.push("");
    } else {
/* 133 */       Object parent = this.m_objs.peek();
/* 134 */       String tag = (String)this.m_tags.peek();

/* 136 */       if (parent instanceof ServerAlarmRuleConfig) {
/* 137 */         if ("rules".equals(tag)) {
/* 138 */           this.m_objs.push(parent);
        } else {
/* 140 */           throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
        }
/* 142 */       } else if (parent instanceof Rule) {
/* 143 */         if ("conditions".equals(tag)) {
/* 144 */           this.m_objs.push(parent);
        } else {
/* 146 */           throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
        }
/* 148 */       } else if (parent instanceof Condition) {
/* 149 */         if ("sub-conditions".equals(tag)) {
/* 150 */           this.m_objs.push(parent);
        } else {
/* 152 */           throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
        }
      } else {
/* 155 */         throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
      }
    }
  }
  protected void onArrayEnd() {
/* 160 */     this.m_objs.pop();
/* 161 */     this.m_tags.pop();
  }

  
/* 165 */   protected void onName(String name) { this.m_tags.push(name); }

  
  protected void onObjectBegin() {
/* 169 */     if (isTopLevel()) {
/* 170 */       this.m_objs.push(createRootEntity());
/* 171 */       this.m_tags.push("");
    } else {
/* 173 */       Object parent = this.m_objs.peek();
/* 174 */       String tag = (String)this.m_tags.peek();

/* 176 */       if (parent instanceof ServerAlarmRuleConfig) {
/* 177 */         if ("rules".equals(tag)) {
/* 178 */           Rule rules = new Rule();

/* 180 */           this.m_linker.onRule((ServerAlarmRuleConfig)parent, rules);
/* 181 */           this.m_objs.push(rules);
/* 182 */           this.m_tags.push("");
        } else {
/* 184 */           throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
        }
/* 186 */       } else if (parent instanceof Rule) {
/* 187 */         if ("conditions".equals(tag)) {
/* 188 */           Condition conditions = new Condition();

/* 190 */           this.m_linker.onCondition((Rule)parent, conditions);
/* 191 */           this.m_objs.push(conditions);
/* 192 */           this.m_tags.push("");
        } else {
/* 194 */           throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
        }
/* 196 */       } else if (parent instanceof Condition) {
/* 197 */         if ("sub-conditions".equals(tag)) {
/* 198 */           SubCondition subConditions = new SubCondition();

/* 200 */           this.m_linker.onSubCondition((Condition)parent, subConditions);
/* 201 */           this.m_objs.push(subConditions);
/* 202 */           this.m_tags.push("");
        } else {
/* 204 */           throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
        }
      } else {
/* 207 */         throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
      }
    }
  }
  
  protected void onObjectEnd() {
/* 213 */     this.m_tags.pop();

/* 215 */     Object entity = this.m_objs.pop();

/* 217 */     if (isTopLevel()) {
/* 218 */       this.m_entities.add((T) entity);
    }
  }
  
  protected void onValue(String value) {
/* 223 */     Object parent = this.m_objs.peek();
/* 224 */     String tag = (String)this.m_tags.pop();

/* 226 */     if (parent instanceof ServerAlarmRuleConfig) {
/* 227 */       parseForServerAlarmRuleConfig((ServerAlarmRuleConfig)parent, tag, value);
/* 228 */     } else if (parent instanceof Rule) {
/* 229 */       parseForRule((Rule)parent, tag, value);
/* 230 */     } else if (parent instanceof Condition) {
/* 231 */       parseForCondition((Condition)parent, tag, value);
/* 232 */     } else if (parent instanceof SubCondition) {
/* 233 */       parseForSubCondition((SubCondition)parent, tag, value);
    } else {
/* 235 */       throw new RuntimeException(String.format("Unknown tag(%s) under %s!", new Object[] { tag, parent }));
    }
  }
  
  private void parse(JsonReader reader) throws IOException {
    try {
/* 241 */       reader.parse(this);
/* 242 */     } catch (EOFException e) {
/* 243 */       if (this.m_objs.size() > 1) {
/* 244 */         throw new EOFException(String.format("Unexpected end while parsing json! tags: %s.", new Object[] { this.m_tags }));
      }
    }
    
/* 248 */     this.m_linker.finish();
  }
  
  public void parseForCondition(Condition condition, String tag, String value) {
/* 252 */     if (!"sub-conditions".equals(tag))
    {
/* 254 */       if ("interval".equals(tag)) {
/* 255 */         condition.setInterval(value);
/* 256 */       } else if ("duration".equals(tag)) {
/* 257 */         condition.setDuration(((Integer)convert(Integer.class, value, Integer.valueOf(0))).intValue());
/* 258 */       } else if ("alert-type".equals(tag)) {
/* 259 */         condition.setAlertType(value);
      } else {
/* 261 */         throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, condition, this.m_tags }));
      }  }
  }
  
  public void parseForRule(Rule rule, String tag, String value) {
/* 266 */     if (!"conditions".equals(tag))
    {
/* 268 */       if ("start-time".equals(tag)) {
/* 269 */         rule.setStartTime(value);
/* 270 */       } else if ("end-time".equals(tag)) {
/* 271 */         rule.setEndTime(value);
      } else {
/* 273 */         throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, rule, this.m_tags }));
      }  }
  }
  
  public void parseForServerAlarmRuleConfig(ServerAlarmRuleConfig serverAlarmRuleConfig, String tag, String value) {
/* 278 */     if (!"rules".equals(tag))
    {
/* 280 */       if ("id".equals(tag)) {
/* 281 */         serverAlarmRuleConfig.setId(value);
      } else {
/* 283 */         throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, serverAlarmRuleConfig, this.m_tags }));
      }  }
  }
  
  public void parseForSubCondition(SubCondition subCondition, String tag, String value) {
/* 288 */     if ("type".equals(tag)) {
/* 289 */       subCondition.setType(value);
/* 290 */     } else if ("value".equals(tag)) {
/* 291 */       subCondition.setValue(value);
    } else {
/* 293 */       throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, subCondition, this.m_tags }));
    }
  }
  
  static class JsonReader { private Reader m_reader;
    private char[] m_buffer;
    
    public JsonReader(Reader reader) {
/* 301 */       this.m_buffer = new char[2048];






/* 308 */       this.m_reader = reader;
    }
    private int m_size; private int m_index;
    private char next() throws IOException {
/* 312 */       if (this.m_index >= this.m_size) {
/* 313 */         this.m_size = this.m_reader.read(this.m_buffer);
/* 314 */         this.m_index = 0;

/* 316 */         if (this.m_size == -1) {
/* 317 */           throw new EOFException();
        }
      }
      
/* 321 */       return this.m_buffer[this.m_index++];
    }
    
    public void parse(DefaultJsonParser parser) throws IOException {
/* 325 */       StringBuilder sb = new StringBuilder();
/* 326 */       boolean flag = false;
      
      while (true) {
/* 329 */         char ch = next();

/* 331 */         switch (ch) {
          case '\t':
          case '\n':
          case '\r':
          case ' ':
            continue;
          case '{':
/* 338 */             parser.onObjectBegin();
/* 339 */             flag = false;
            continue;
          case '}':
/* 342 */             if (flag) {
/* 343 */               parser.onValue(sb.toString());
/* 344 */               sb.setLength(0);
            }
            
/* 347 */             parser.onObjectEnd();
/* 348 */             flag = false;
            continue;
          case '"':
          case '\'':
            while (true) {
/* 353 */               char ch2 = next();

/* 355 */               if (ch2 != ch) {
/* 356 */                 if (ch2 == '\\') {
/* 357 */                   char ch3 = next();

/* 359 */                   switch (ch3) {
                    case 't':
/* 361 */                       sb.append('\t');
                      continue;
                    case 'r':
/* 364 */                       sb.append('\r');
                      continue;
                    case 'n':
/* 367 */                       sb.append('\n');
                      continue;
                  }
/* 370 */                   sb.append(ch3);
                  
                  continue;
                }
/* 374 */                 sb.append(ch2); continue;
              }  break;
            }
/* 377 */             if (!flag) {
/* 378 */               parser.onName(sb.toString());
            } else {
/* 380 */               parser.onValue(sb.toString());
/* 381 */               flag = false;
            }
            
/* 384 */             sb.setLength(0);
            continue;



          
          case ':':
/* 391 */             if (sb.length() != 0) {
/* 392 */               parser.onName(sb.toString());
/* 393 */               sb.setLength(0);
            }
            
/* 396 */             flag = true;
            continue;
          case ',':
/* 399 */             if (sb.length() != 0) {
/* 400 */               if (!flag) {
/* 401 */                 parser.onName(sb.toString());
              } else {
/* 403 */                 parser.onValue(sb.toString());
              }
              
/* 406 */               sb.setLength(0);
            }
            
/* 409 */             flag = false;
            continue;
          case '[':
/* 412 */             parser.onArrayBegin();
/* 413 */             flag = false;
            continue;
          case ']':
/* 416 */             parser.onArrayEnd();
/* 417 */             flag = false;
            continue;
        }
/* 420 */         sb.append(ch);
      }
    } }

}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-alarm-3.0.0.jar!/com/dianping/cat/alarm/server/transform/DefaultJsonParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */